<template>
  <div>
    <el-dialog
      :title="title"
      width="652px"
      :visible.sync="isShow"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="项目编码" prop="code" v-if="form.id">
          <el-input :disabled="true" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input placeholder="请填写项目名称" v-model="form.name" maxlength="24"></el-input>
        </el-form-item>
        <el-form-item label="项目状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="(item, index) in projectStatusList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目logo" prop="logo">
          <FileUpload
            ref="FileUpload"
            accept="image/jpeg,image/png"
            @uploadSuccess="uploadSuccess"
            :imgUrl="form.logo"
          ></FileUpload>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" maxlength="150" show-word-limit :rows="10" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit()">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import FileUpload from '@/components/FileUpload/index.vue'
import { STATUS_LIST } from '@/constants/baseModel.js'
export default {
  name: 'AddEditPop',
  data() {
    return {
      isShow: false,
      rules: {
        name: [{ required: true, message: '请输入项目名称' }],
        logo: [{ required: true, message: '请上传logo' }],
      },
      projectStatusList: STATUS_LIST,
      form: {
        id: '',
        // 项目编码
        code: '',
        // 项目名称
        name: '',
        logo: '',
        // 项目状态
        status: '1',
        // 描述
        description: '',
      },
    }
  },
  components: { FileUpload },
  computed: {
    title() {
      return this.form.id ? '编辑项目' : '新增项目'
    },
  },
  methods: {
    show(item) {
      this.isShow = true
      this.form.id = (item && item.id) || ''
      this.form.name = (item && item.name) || ''
      this.form.code = (item && item.code) || ''
      this.form.logo = (item && item.logo) || ''
      this.form.status = (item && item.status) || '1'
      this.form.description = (item && item.description) || ''
    },
    submit() {
      this.$refs['form'].validate(async valid => {
        if (!valid) return
        await this.sendSubmitRequest()
        this.resetData()
        this.$emit('saveSuccess', true)
      })
    },
    async sendSubmitRequest() {
      const url = this.form.id ? `/app/${this.form.id}/update` : '/app/add'
      const method = this.form.id ? 'put' : 'post'
      await this.$http[method](url, this.form)
    },
    handleClose() {
      this.resetData()
      this.$emit('saveSuccess')
    },
    resetData() {
      this.isShow = false
      this.form.id = ''
      this.form.code = ''
      this.form.name = ''
      this.form.logo = ''
      this.form.status = '1'
      this.form.description = ''
      this.$refs['form'].resetFields()
    },
    uploadSuccess(res, file) {
      console.log('上传完后父组件', res, file)
      this.form.logo = res.data
      this.$refs.form.validateField('logo')
    },
  },
}
</script>

<style lang="scss" scoped>
.btn {
  width: 400px;
}
::v-deep {
  .el-input {
    width: 400px;
  }
  .el-input__inner {
    width: 400px;
  }
  .el-textarea {
    width: 400px;
  }
}
</style>
